package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    //图片
    @Autowired
    private GraphInfoService graphInfoService;
    //属性信息
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    //配套
    @Autowired
    private RoomFacilityService roomFacilityService;
    //标签
    @Autowired
    private RoomLabelService roomLabelService;
    //支付方式
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    //租期
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    //直接mapper层，防止注入循环
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapperr;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public void customizeSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        boolean b = roomSubmitVo.getId() != null;
        saveOrUpdate(roomSubmitVo);
        if (b) {
            //删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =
                    new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomSubmitVo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //删除属性列表
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper =
                    new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //删除配套信息
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper =
                    new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            //删除标签信息
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper =
                    new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //删除支付列表
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper =
                    new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //删除租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper =
                    new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        //插入图片
        //获取一个GraphVo对象的列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        //检查列表是否为空，不为空执行后续代码
        if (!CollectionUtils.isEmpty(graphVoList)) {
            //创建一个新的 arraylist 用来存储 GrapInfo 对象
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            //开始遍历 GraphInfo 中每一个 GraphVo 对象
            for (GraphVo graphInfo : graphVoList) {
                //为每个 GraphVo 对象创建一个新的 GraphInfo 对象
                GraphInfo info = new GraphInfo();
                info.setItemId(roomSubmitVo.getId());
                info.setItemType(ItemType.ROOM);
                info.setName(graphInfo.getName());
                info.setUrl(graphInfo.getUrl());
                //将新的 GraphInfo 对象添加到 graphInfos 列表中
                graphInfos.add(info);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            ArrayList<RoomAttrValue> attrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                attrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(attrValues);
        }
        //插入配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<RoomFacility> facilityInfos = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                facilityInfos.add(roomFacility);
            }
            roomFacilityService.saveBatch(facilityInfos);
        }
        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> labels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                labels.add(roomLabel);
            }
            roomLabelService.saveBatch(labels);
        }
        //支付
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> paymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                paymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(paymentTypes);
        }
        //租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> leaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                leaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(leaseTerms);
        }


    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //房间信息
        RoomInfo byId = this.getById(id);
        if (byId==null){return null;}
        //依次查询
        //公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartment(id);
        //图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        //属性
        List<AttrValueVo> AttrValueList =  attrValueMapper.selectArrtById(id);
        //配置
        List<FacilityInfo> FacilityList = facilityInfoMapper.selectFacilityById(id);
        //标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelById(id);
        //支付
        List<PaymentType> paymentTypeList = paymentTypeMapperr.selectPayById(id);
        //租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermById(id);

        //属性赋值
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(byId,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(AttrValueList);
        roomDetailVo.setFacilityInfoList(FacilityList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //直接删
        removeById(id);
        //删除图片信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.ROOM));
        //删除属性信息
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        //删除配置信息
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        //删除标签信息
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        //删除支付信息
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        //删除租期信息
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));


    }

    @Override
    public void pageRoomItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.pageRoomItem(page,queryVo);
    }
}
